home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / SORTING.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  3KB  |  107 lines

  1. /****************************************************************************
  2.  *
  3.  *    Program Name : SORTING.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *    This program sorts in ascending order, the minterms in a given array.
  8.  *
  9.  *    Returns pointer to the sorted array of minterms
  10.  *
  11.  * --------------------------------------------------------------------------
  12.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  13.  *    University.
  14.  *
  15.  *    You are free to use, copy and distribute this software and its
  16.  *    documentation providing that:
  17.  *
  18.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  19.  *
  20.  *        IT IS NOT MODIFIED IN ANY WAY.
  21.  *
  22.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  23.  *
  24.  *    This program is provided "AS IS" without any warranty, expressed or
  25.  *    implied, including but not limited to fitness for any particular
  26.  *    purpose.
  27.  *
  28.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  29.  *    appreciated. Please send to:
  30.  *
  31.  *        Boon-Tiong Tan or Othman Bin Ahmad
  32.  *        School of EEE
  33.  *        Nanyang Technological University
  34.  *        Nanyang Avenue
  35.  *        Singapore 2263
  36.  *        Republic of Singapore
  37.  *
  38.  ***************************************************************************/
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43.  
  44. unsigned char   *sorting(b)
  45. unsigned char     *b;
  46.  
  47. {
  48.    unsigned short  mb, i;
  49.    unsigned char   nspm, *hold1, *hold2, *temp, j;
  50.          int   test;
  51.  
  52.  
  53.    nspm = *(b+3);                             /* no. of bytes/minterm */
  54.    mb = *(b+2)<<8 | *(b+1);                   /* no. of minterms in b */
  55.  
  56.    hold1 = (unsigned char *)  malloc(nspm);   /* temporary storage */
  57.    if (hold1 == 0)
  58.       {
  59.      printf("Out of memory -- SORTING, *hold1\n");
  60.      printf("Program Terminated - 1");
  61.      exit(0);
  62.       }
  63.  
  64.    hold2 = (unsigned char *)  malloc(nspm);   /* temporary storage */
  65.    if (hold2 == 0)
  66.       {
  67.      printf("Out of memory -- SORTING, *hold2\n");
  68.      printf("Program Terminated - 2");
  69.      exit(0);
  70.       }
  71.  
  72.    temp = (unsigned char *)  malloc(nspm);   /* temporary storage */
  73.    if (temp == 0)
  74.       {
  75.      printf("Out of memory -- SORTING, *temp \n");
  76.      printf("Program Terminated - 3");
  77.      exit(0);
  78.       }
  79.  
  80.    while (mb-- > 1)                      /* bubble sort algorithm */
  81.       {
  82.      for (i=0; i<mb; i++)            /* do for decreasing order of mb */
  83.         {
  84.            for (j=nspm; j>0; j--)    /* rearrange for nspm>1 */
  85.           {
  86.              *(hold1+nspm-j) = *(b+4+nspm*i+j-1);
  87.              *(hold2+nspm-j) = *(b+4+nspm*(i+1)+j-1);
  88.           }
  89.  
  90.            test = memcmp(hold1, hold2, nspm);
  91.            if (test > 0)                            /* hold1 > hold2 */
  92.           {                                     /* swap minterms */
  93.              memcpy(temp, (b+4+nspm*i), nspm);
  94.              memcpy((b+4+nspm*i), (b+4+nspm*(i+1)), nspm);
  95.              memcpy((b+4+nspm*(i+1)), temp, nspm);
  96.           }
  97.         }
  98.       }
  99.    free(hold1);                        /* free pointers */
  100.    free(hold2);
  101.    free(temp);
  102.  
  103.    return(b);                          /* return sorted array */
  104. }
  105.  
  106.  
  107.